package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: Hes
 * @description 师资信息接口实现
 * @date: 2023/6/20 22:10
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {
    @Autowired
    CourseTeacherMapper courseTeacherMapper;


    /**
     * 查询师资信息
     *
     * @param courseId
     * @return
     */
    @Override
    public List<CourseTeacher> list(Long courseId) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId, courseId);
        List<CourseTeacher> teacherList = courseTeacherMapper.selectList(queryWrapper);
        if (teacherList == null) {
            XueChengPlusException.cast("这个课程没有师资信息");
        }
        return teacherList;
    }

    /**
     * @param courseTeacher
     * @return
     */
    @Override
    @Transactional
    public CourseTeacher courseTeacher(Long companyId, CourseTeacher courseTeacher) {
        Long courseId = courseTeacher.getCourseId();
        String teacherName = courseTeacher.getTeacherName();
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId, courseId)
                .eq(CourseTeacher::getTeacherName, teacherName);
        CourseTeacher courseTeacherFromDB = courseTeacherMapper.selectOne(queryWrapper);
        if (courseTeacherFromDB != null) {
            return courseTeacherFromDB;
        }
        courseTeacher.setCreateDate(LocalDateTime.now());
        courseTeacherMapper.insert(courseTeacher);
        return courseTeacher;
    }

    /**
     * 修改教师信息
     *
     * @param companyId
     * @param courseTeacher
     * @return
     */
    @Override
    @Transactional
    public CourseTeacher modifyCourseTeacher(Long companyId, CourseTeacher courseTeacher) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<CourseTeacher>()
                .eq(CourseTeacher::getCourseId, courseTeacher.getCourseId())
                .eq(CourseTeacher::getTeacherName, courseTeacher.getTeacherName());
        CourseTeacher courseTeacherFromDB = courseTeacherMapper.selectOne(queryWrapper);
        if (courseTeacherFromDB != null){
            XueChengPlusException.cast("这位老师正在教这门课程，不能再添加他的信息");
        }
        int update = courseTeacherMapper.updateById(courseTeacher);
        CourseTeacher teacher = courseTeacherMapper.selectById(courseTeacher);
        System.out.println(teacher);
        return teacher;
    }

    /**
     * 删除教师信息
     * @param courseId
     * @param teacherId
     */
    @Override
    @Transactional
    public void delTeacher(Long courseId, Long teacherId) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<CourseTeacher>()
                .eq(CourseTeacher::getCourseId, courseId)
                .eq(CourseTeacher::getId, teacherId);
        CourseTeacher courseTeacher = courseTeacherMapper.selectOne(queryWrapper);
        if (courseTeacher==null){
            XueChengPlusException.cast("没有这位教师信息");
        }
        courseTeacherMapper.deleteById(teacherId);
    }
}
